package wu.controller;

import org.apache.ibatis.session.SqlSession;
import wu.mapper.StudentMapper;
import wu.pojo.NiuStudent;
import wu.util.MyBatisUtils;

import java.util.*;

public class SetNiuStudents {
    private static final SqlSession sqlSession = MyBatisUtils.getSession();
    private static final StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要关联查询的周赛周数：");
        int num = sc.nextInt();
        List<NiuStudent> list2=new  ArrayList<NiuStudent>();
        List<Integer> niuIds = studentMapper.getNiuIds();
        for (Integer niuId : niuIds) {
            NiuStudent niuStudent = studentMapper.getStudentByIdAndTableName("user_table_" + num, niuId);
            // 处理查询到的学生和相关 User_niu 信息
            if (niuStudent != null) {
                list2.add(niuStudent);
            }
        }
        // 创建一个Comparator接口实现，用于比较两个NiuStudent对象的niu_total_score属性
        Comparator<NiuStudent> comparator = new Comparator<NiuStudent>() {
            @Override
            public int compare(NiuStudent s1, NiuStudent s2) {
                if (s1.getNiu_total_score() == null) {
                    return 1;
                }
                if (s2.getNiu_total_score() == null) {
                    return -1;
                }
                // 将字符串转换为double类型后进行比较
                double score1 = Double.parseDouble(s1.getNiu_total_score());
                double score2 = Double.parseDouble(s2.getNiu_total_score());
                if (score2 > score1) {
                    return 1;
                } else if (score2 < score1) {
                    return -1;
                } else {
                    return s1.getNiu_time().compareTo(s2.getNiu_time());
                }
            }
        };
        // 使用Collections.sort方法对列表进行排序
        Collections.sort(list2, comparator);
        for (NiuStudent niuStudent : list2) { // 处理排序后的学生信息
            System.out.println(niuStudent); // 输出学生信息
        }
    }
}
